package com.rem.chat.controller;

import com.rem.chat.aop.annotation.GlobalInterceptor;
import com.rem.chat.common.constants.Constants;
import com.rem.chat.common.utils.JacksonUtil;
import com.rem.chat.common.utils.RedisUtil;
import com.rem.chat.pojo.request.ApplyAddRequest;
import com.rem.chat.pojo.request.ApplyDealWithRequest;
import com.rem.chat.pojo.request.PageRequest;
import com.rem.chat.pojo.request.ContactSearchRequest;
import com.rem.chat.pojo.response.*;
import com.rem.chat.service.ContactService;
import com.rem.chat.service.dto.UserLoginDTO;
import jakarta.annotation.Nullable;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.constraints.NotNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author: Themberfue
 * @date: 2025/6/6 20:29
 * @description:
 */
@RestController
@RequestMapping("/contact")
public class ContactController {

    private static final Logger log = LoggerFactory.getLogger(ContactController.class);

    @Resource(name = "contactServiceImpl")
    ContactService contactService;

    @Resource
    RedisUtil redisUtil;

    @PutMapping("/searchContact")
    @GlobalInterceptor
    public CommonResult<List<SearchContactResponse>> searchContact(@RequestBody @Validated ContactSearchRequest request) {
        log.info("searchContact contactId:{}", JacksonUtil.writeValueAsString(request));

        return CommonResult.success(
                contactService.searchContact(request));
    }

    @PostMapping("/applyAdd")
    @GlobalInterceptor
    public CommonResult<Boolean> applyAdd(HttpServletRequest request,
                                          @RequestBody @Validated ApplyAddRequest param) {
        log.info("applyAdd ApplyAddRequest:{}", JacksonUtil.writeValueAsString(param));

        // Step 获取用户 Token (拦截器已经提前处理，无需做过多校验)
        UserLoginDTO user = getUserLoginDTO(request);

        return CommonResult.success(
                contactService.applyAdd(user, param));
    }

    @GetMapping("/loadApply")
    @GlobalInterceptor
    public CommonResult<LoadAllApplyResponse> loadApply(HttpServletRequest request,
                                                        @RequestBody @Nullable @Validated PageRequest param) {
        log.info("loadApply PageRequest:{}", JacksonUtil.writeValueAsString(param));

        // Step 获取用户 Token (拦截器已经提前处理，无需做过多校验)
        UserLoginDTO user = getUserLoginDTO(request);

        return CommonResult.success(
                contactService.loadApply(user.getUserId(), param));
    }

    @PostMapping("/dealWithApply")
    @GlobalInterceptor
    public CommonResult<Boolean> dealWithApply(HttpServletRequest request,
                                               @RequestBody @Validated ApplyDealWithRequest param) {
        
        log.info("dealWithApply ApplyDealWithRequest:{}", JacksonUtil.writeValueAsString(param));

        // Step 获取用户 Token (拦截器已经提前处理，无需做过多校验)
        UserLoginDTO user = getUserLoginDTO(request);

        return CommonResult.success(
                contactService.dealWithApply(user.getUserId(), param));
    }

    @GetMapping("/loadContact")
    @GlobalInterceptor
    public CommonResult<List<LoadContactResponse>> loadContact(HttpServletRequest request,
                                                               @NotNull Integer contactType) {
        
        log.info("loadContact contactType:{}", contactType);

        // Step 获取用户 Token (拦截器已经提前处理，无需做过多校验)
        UserLoginDTO user = getUserLoginDTO(request);

        return CommonResult.success(
                contactService.loadContact(user.getUserId(), contactType));
    }

    @GetMapping("/getContactInfo")
    @GlobalInterceptor
    public CommonResult<GetContactInfoResponse> getContactInfo(HttpServletRequest request,
                                                               @NotNull String contactId) {
        
        log.info("getContactInfo contactId:{}", contactId);

        // Step 获取用户 Token (拦截器已经提前处理，无需做过多校验)
        UserLoginDTO user = getUserLoginDTO(request);

        return CommonResult.success(
                contactService.getContactInfo(user.getUserId(), contactId));
    }

    private UserLoginDTO getUserLoginDTO(HttpServletRequest request) {
        String token = request.getHeader("token");
        String user = redisUtil.get(Constants.REDIS_KEY_WS_TOKEN + token);
        log.info("user: {}", user);
        return JacksonUtil.readValue(user, UserLoginDTO.class);
    }
}
